package itmayiedu.api.service.impl;

import com.itmayiedu.api.service.impl.IOrderService;
import com.itmayiedu.base.BaseApiService;
import com.itmayiedu.base.ResponseBase;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import entity.UserEntity;
import itmayiedu.api.feign.MemberServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 会员接口实现类
 * @author: liucancan
 * @create: 2018-11-12 14:23
 **/
@RestController
public class OrderServiceImpl extends BaseApiService implements IOrderService {

    @Autowired
    private MemberServiceFeign memberServiceFeign;

    @RequestMapping("/orderToMember")
    public String orderToMember() {
        UserEntity member = memberServiceFeign.getMember();
        return member.toString();
    }

    /**
     * 没有解决服务的雪崩效应
     *
     * @return
     */
    @RequestMapping("/orderToMemberUserInfo")
    public ResponseBase orderToMemberUserInfo() {
        return memberServiceFeign.getUserInfo();
    }


    /**
     * 解决服务的雪崩效应
     *
     * @return
     */
    @HystrixCommand(fallbackMethod = "orderToMemberUserInfoHystrixFallback")
    @RequestMapping("/orderToMemberUserInfoHystrix")
    public ResponseBase orderToMemberUserInfoHystrix() {
        System.out.println("线程池名称：" + Thread.currentThread().getName());
        return memberServiceFeign.getUserInfo();
    }

    public ResponseBase orderToMemberUserInfoHystrixFallback() {
        return setResultSuccess("服务降级生效了,服务器忙请稍后重试");
    }

    @RequestMapping("/orderInfo")
    public ResponseBase orderInfo() {
        System.out.println("线程池名称：" + Thread.currentThread().getName());
        return setResultSuccess();
    }

    /**
     * 第二种使用hystrix方式：在service层开启独立线程池，memberServiceFeign.getUserInfo()，为
     *
     * @return
     */
    @RequestMapping("/orderToMemberUserInfoHystrixDemo2")
    public ResponseBase orderToMemberUserInfoHystrixDemo2() {
        // 这行代码为主线程
        System.out.println("线程池名称：" + Thread.currentThread().getName());
        // memberServiceFeign.getUserInfo()是在新的线程池中处理
        return memberServiceFeign.getUserInfo();
    }
}
